<?php


namespace App\Admin\Forms;


use App\Models\WxAuthentication;
use App\Models\WxFeedback as FeedbackModel;
use App\Models\WxNotice as NoticeModel;
use App\Models\WxUser;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class AuthenticationForms extends Form implements LazyRenderable
{
    use LazyWidget;


    /**
     * 主题颜色.
     *
     * @var array
     */
    protected $colors = [
        'default' => '深蓝',
        'blue' => '蓝',
//        'blue-light' => '浅蓝',
//        'blue-dark'  => '深蓝',
        'green' => '绿',
    ];

    /**
     * 处理表单请求.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {


        $uid = WxAuthentication::where('id', $input['id'])->value('user_id');
        $type = $input['type'];
        if($type == 1){
            WxAuthentication::where('id',$input['id'])->update([
                'authentication_state'=>1
            ]);
            WxUser::where('id',$uid)->update([
               'is_authentication'=>1
            ]);
            $noticeModel = new NoticeModel;
            $noticeModel->user_id = $uid;
            $noticeModel->title = '认证处理结果';
            $noticeModel->content = '认证成功，您提交的认证已经审核通过';
            $noticeModel->is_read = 0;
            $noticeModel->notice_type = 0;
            $noticeModel->save();

        }else{
            WxAuthentication::where('id',$input['id'])->update([
                'authentication_state'=>2,
                'overrule_content'=>$input['overrule_content']
            ]);
            $noticeModel = new NoticeModel;
            $noticeModel->user_id = $uid;
            $noticeModel->title = '认证处理结果';
            $noticeModel->content = '认证失败,'.$input['overrule_content'];
            $noticeModel->is_read = 0;
            $noticeModel->notice_type = 0;
            $noticeModel->save();
        }



        return $this->response()
            ->success('操作完成.')
            ->refresh();
    }

    /**
     * 构建表单.
     */
    public function form()
    {

        $this->hidden('id')->value($this->payload["id"]);
        $this->radio('type','操作')
            ->when(1, function (Form $form) {

            })
            ->when(2, function (Form $form) {
                $form->textarea('overrule_content', "回复内容");
            })
            ->options([
                1 => '审核通过',
                2 => '驳回',

            ])
            ->default(1);

    }
}
